package com.example.ex_intent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    //@Override
    RadioButton r1 = null;
    RadioButton r2 = null;
    RadioButton r3 = null;
    RadioButton r4 = null;
    RadioGroup  rg = null;
    RadioButton currentButton = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg = (RadioGroup)findViewById(R.id.radioGroup);
        r1 = (RadioButton)findViewById(R.id.A);
        r2 = (RadioButton)findViewById(R.id.B);
        r3 = (RadioButton)findViewById(R.id.C);
        r4 = (RadioButton)findViewById(R.id.D);
        r1.setClickable(true);

        rg.setOnCheckedChangeListener(mChangeRadio);
        Button btn_sure = (Button)findViewById(R.id.btn1);
        Button btn_cancel = (Button)findViewById(R.id.btn2);
        btn_sure.setOnClickListener(new btn_sure());
        btn_cancel.setOnClickListener(new btn_cancel());
    }

    class btn_sure implements View.OnClickListener {

        @Override
        public void onClick(View v){
            String ans = "";
           if(currentButton.getText().equals("篮球")){
                setTitle("你的爱好是：篮球");
                ans = "篮球";
            }else if(currentButton.getText().equals("足球")){
               setTitle("你的爱好是：足球");
               ans = "足球";
           }else if(currentButton.getText().equals("吉他")){
               setTitle("你的爱好是：吉他");
               ans = "吉他";
           }
            else{
                setTitle("你的爱好是：跳舞");
                ans = "跳舞";
            }

            Intent intent = new Intent(MainActivity.this,ResultActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("ans",ans);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    class btn_cancel implements View.OnClickListener{
        @Override
        public void onClick(View v){
            rg.clearCheck();
            setTitle("");
        }
    }

    private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == r1.getId()) {
                currentButton = r1;
            } else if(checkedId == r2.getId()) {
                currentButton = r2;
            }else if(checkedId == r3.getId()){
                currentButton = r3;
            }else{
                currentButton = r4;
            }

        }
    };

}
